use log::info;
use log4rs;
use log_config;
use log_parser::LogInfo;
use mongodb::sync::Client;
use std::env;

fn main() {
    let dir = env::current_dir().unwrap();
    let log = format!("{}/log.toml", &dir.display());
    log4rs::init_file(&log, Default::default()).unwrap();
    let config = log_config::Config::load();

    let client = Client::with_uri_str(config.mongo()).unwrap();
    let database = client.database("logs");
    let collection = database.collection("log_info");

    let args: Vec<String> = env::args().collect();
    println!("{:?}", args);
    if args.len() < 2 {
        panic!("加载日志参数个数错误！");
    }
    let filename = args[1].clone();

    if args.len() == 3 && args[2].clone() == "u" {
        // 有参数的情况不清空原先的记录
    } else {
        info!("删除mongodb记录");
        collection.drop(None).unwrap();
    }

    //加载日志
    LogInfo::load_trace(&filename, &collection);    
}
